############# ########### ####### ##### ### ##
#
# Copyright (C) 2010 The Hursk Group, LLC
#
# This program is free software: you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (See file named COPYING).  If not, see 
# <http://www.gnu.org/licenses/>.
# 
############# ########### ####### ##### ### ##

#  ~/path/to/audit% setenv debug 1
# ~/path/to/audit% awk -f ~/Desktop/Utils\ New/patcher.awk -v debug=${debug} pass=1 pkginfo.long.out pass=2 showrev_a.out

BEGIN {
}

# pass=1 pkginfo.long.out
pass == 1 {
	RS=""
	FS="\n"
	SUBSEP="___"
	# XXX STUPID fix if first record. some reason it ends after one line
	if (FNR == "1" ) {
		record_1 = $0
		field = substr($0,1,10)
		gsub(" ","",field)
		data = substr($0,14)
		pkginst_arr[data] = data
		# main array
		main_arr[data,0] = data
	}
	# Break up the fields
	for (x=1;x<=NF;x++) {
		# Don't care about file stuff
		if ($x ~ "^[ ][ ]?[ ]?[ ]?[ ]?[ ][A-Z].*:  ") {
			# XXX STUPID fix if first record.
			if (FNR == "2") pkginst = record_1
			field = substr($x,1,10)
			gsub(" ","",field)
			data = substr($x,14)
			if (debug >= 2) print "FF: " field " DD: " data
			if (field == "PKGINST") {
				# index array
				pkginst_arr[data] = data
				# main array
				main_arr[data,0] = data
				# main array partial index key
				pkginst = data
			}
			if (field == "VENDOR") main_arr[pkginst,1] = data
			if (field == "NAME") main_arr[pkginst,2] = data
		}
	}
}

# pass=2 showrev_a.out
pass == 2 {
	RS="\n"
	FS=":"
	#SUBSEP="___"
	if ( $0 ~ "^Patch" ) {
		# cleanup fields of interest; $2 = patches, $6 = packages
		gsub(" ","",$6)
		gsub("Obsoletes","",$2)
		gsub(" ","",$2)
		# break up packages affected by patch
		pkg_count = split($6,patch_pkginst_arr,",")
		if (pkg_count == "0") {
			print "Error, pkg_count == 0: "$0
			exit 1
		}
		# Force an array if there was only one package for the patch
		if (pkg_count == "1") patch_pkginst_arr[1] = $6

		# stuff the main array with the patch info
		for (pkgs in patch_pkginst_arr) {
			if (main_arr[patch_pkginst_arr[pkgs],0] == "") {
				print "Error, package does not exist in main array: " $6
			}
			if (main_arr[patch_pkginst_arr[pkgs],3] == "") {
				main_arr[patch_pkginst_arr[pkgs],3] = $2
			} else {
				main_arr[patch_pkginst_arr[pkgs],3] = main_arr[patch_pkginst_arr[pkgs],3] SUBSEP $2
			}
			if (debug >= "2") print main_arr[patch_pkginst_arr[pkgs],0] " " main_arr[patch_pkginst_arr[pkgs],3]
		}
		delete patch_pkginst_arr
	}
}

END {
	#for (elements in main_arr) print main_arr[elements,0]
	for (elements in pkginst_arr) print main_arr[elements,0] SUBSEP main_arr[elements,1] SUBSEP main_arr[elements,2] SUBSEP main_arr[elements,3]
	#for (elements in main_arr) print main_arr[elements]
}

